//
//  ViewController.m
//  LessonINC01
//
//  Created by zhaiyingwei on 16/1/31.
//  Copyright © 2016年 zhaiyingwei. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    NSInteger count;
    UINavigationBar *navigationBar;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 44)];
    [self.view addSubview:navigationBar];
    [self push];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)push
{
    count++;
    [navigationBar pushNavigationItem:[self makeNavItem] animated:YES];
}

-(void)pop
{
    if (count>1) {
        [navigationBar popNavigationItemAnimated:YES];
        count--;
    }else{
        UIAlertController *alertC=[UIAlertController alertControllerWithTitle:@"系统提示" message:@"最后一个不能再删了。" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"AlertView");
        }];
        [alertC addAction:alertAction];
        [self presentViewController:alertC animated:YES completion:nil];
    }
}

-(UINavigationItem *)makeNavItem
{
    UINavigationItem *navigationItem=[[UINavigationItem alloc]initWithTitle:nil];
    UIBarButtonItem *leftButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(push)];
    UIBarButtonItem *rigthButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pop)];
    navigationItem.title=[NSString stringWithFormat:@"第【%lu】个导航页",count];
    [navigationItem setLeftBarButtonItem:leftButton animated:YES];
    [navigationItem setRightBarButtonItem:rigthButton animated:YES];
    return navigationItem;
}

@end
